import { mobilePattern, numberPattern } from './pattern'
import {getAction} from "@/api/manage";

const phoneValidator = (rule, value, callback) => {
    const val = (value !== undefined && value !== null && (value + '').trim()) || ''
    if (val && !(mobilePattern.test(val))) {
        callback(new Error('请输入正确的手机号'));
    }
    callback();
}
const numberValidator = (rule, value, callback) => {
    const val = (value !== undefined && value !== null && (value + '').trim()) || ''
    if (val && !(numberPattern.test(val))) {
        callback(new Error('请输入正确的数字'));
    }
    callback();
}

const telValidator = (rule,value,callback)=>{
    const val = (value !== undefined && value !== null && (value + '').trim()) || ''
    let flag = null;
    getAction(`fba/customer/checkPhone?phone=${value}`)
        .then(res=>{
            flag = res.result;
            console.log("检查手机号是否存在",flag);
            if (flag === false) {
                callback(new Error('该手机号已经存在'));
            }else {
                callback();
            }

        })
    callback();

}

const intValidator = (rule, value, callback) => {
    const val = (value !== undefined && value !== null && (value + '').trim()) || ''
    if (val && !(/^([1-9]\d*|0)$/.test(val))) {
        console.log(val)
        callback(new Error('请输入数字(整数)'));
    }
    callback();
}

export {
    phoneValidator,
    numberValidator,
    intValidator,
    telValidator,
}
